﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace AlphaSolutionsGP
{
    public class Module
    {
        #region properties
        MySqlConnection connection = new MySqlConnection("server=194.81.104.22;" + "User Id=alpha;" + "database=alpha_db;" + "password=bravo;"); 
        private string _moduleCode;
        private string _courseCode;
        private string _title;
        private string _field;
        private string _description;
        private int _credits;
        private string _staffID;
        private int _numberOfAssignments;
        private int _level;


        #endregion


        #region Constructors
        public Module(string moduleCode, string courseCode, string title, string field
            , string description, int credits, string staffID, int level)
        {
            this._moduleCode = moduleCode;
            this._courseCode = courseCode;
            this._title = title;
            this._field = field;
            this._description = description;
            this._credits = credits;
            this._staffID = staffID;
            this._level = level;
            //MySqlCommand command = connection.CreateCommand();
            //command.CommandText = "select count() from ASSESSMENT where MODULE_module_Code=" + this._moduleCode + "; ";
            ////the execute scalar command should return the first coloumn of the first record or the above command 
            //int assignmentCount = (int)command.ExecuteScalar();
        }
        public Module()
        { 
            
        }
        #endregion


        #region methods

        public void createModule()
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            
            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "INSERT INTO MODULE VALUES('" + this.ModuleCode + "','" + this.CourseCode + "','" + this.Title + "','" + this.Field + "','" + this.Level + "','" + this.Description + "'," + this.Credits + ",'" + this.StaffID + "');";
            MySqlDataReader run = cmd.ExecuteReader();
            connection.Close();
        }

        public void ammendModule(string moduleCode)
        {
            try
            {
                connection.Open();
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Connection failed");
            }
            MySqlCommand cmd = connection.CreateCommand();
            if (!this.CourseCode.Equals("Not assigned")) // 
            {
                cmd.CommandText = "update MODULE set module_Code='" + this.ModuleCode + "',COURSE_course_Code='" + this.CourseCode + "',title='" + this.Title + "',field='" + this.Field + "',level=" + this.Level + ",description='" + this.Description + "',credits=" + this.Credits + ",STAFF_staff_ID='" + this.StaffID + "' where module_Code='" + moduleCode + "';";
                MySqlDataReader run = cmd.ExecuteReader();
            }
            else
            {
                try
                {
                    cmd.CommandText = "update MODULE set module_Code='" + this.ModuleCode + "',COURSE_course_Code=NULL ,title='" + this.Title + "',field='" + this.Field + "',level=" + this.Level + ",description='" + this.Description + "',credits=" + this.Credits + ",STAFF_staff_ID='" + this.StaffID + "' where module_Code='" + moduleCode + "';";
                    MySqlDataReader run = cmd.ExecuteReader();
                }
                catch (Exception ex)
                {
                    //do nothing
                }
            }
            
            connection.Close();
        }

        public void deleteModule()
        {
            try
            {
                connection.Open();
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "DELETE FROM MODULE WHERE module_Code='" + this._moduleCode + "';";
                MySqlDataReader run = cmd.ExecuteReader();
                MessageBox.Show("Module deleted successfully", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                connection.Close();
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to delete the module: " + this._title + ". This is likely because it is currently assigned to a course and must be removed before it can be deleted","Deleting Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            
        }

        #endregion


        #region getters and setters
        public int Level
        {
            get { return _level; }
            set { _level = value; }
        }
        
        public int NumberOfAssignments
        {
            get{ return _numberOfAssignments;}
            set { _numberOfAssignments = value; }

        }
        public string StaffID
        {
            get { return _staffID; }
            set { _staffID = value; }
        }
        

        public int Credits
        {
            get { return _credits; }
            set { _credits = value; }
        }
        


        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        

        public string Field
        {
            get { return _field; }
            set { _field = value; }
        }
        

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        

        public string CourseCode
        {
            get { return _courseCode; }
            set { _courseCode = value; }
        }
        


        public string ModuleCode
        {
            get { return _moduleCode; }
            set { _moduleCode = value; }
        }
        #endregion

    }
}
